﻿CREATE PROC [dbo].[usp_ElectricityHourly_AutoInsert] 
    @ah_hour datetime,
    @ah_kWh decimal(18, 3),
    @ah_meterId int
AS 
	SET NOCOUNT ON 
	SET XACT_ABORT ON  

	IF NOT EXISTS (SELECT * FROM [ElectricityHourly_Auto] WHERE [ah_hour] = @ah_hour AND [ah_meterId] =@ah_meterId )
	
		BEGIN
			PRINT 'NO DATA, INSERTING'	
			INSERT INTO [dbo].[ElectricityHourly_Auto] ([ah_hour], [ah_kWh], [ah_meterId])
			SELECT @ah_hour, @ah_kWh, @ah_meterId
			
			-- Begin Return Select <- do not remove
			SELECT [ah_id], [ah_hour], [ah_kWh], [ah_meterId]
			FROM   [dbo].[ElectricityHourly_Auto]
			WHERE  [ah_id] = SCOPE_IDENTITY()
			-- End Return Select <- do not remove
		END
	ELSE
		BEGIN
			PRINT 'RECORD ALREADY EXISTS'	
			SELECT * FROM [ElectricityHourly_Auto] WHERE [ah_hour] = @ah_hour AND [ah_meterId] =@ah_meterId
		END


